<?php
class delivery_calc_rate
{
        
        public $delivery_Ids;
		
      
	    public $weight;
        public $max;
        public $row;
        public $record;
        public $totalRows;
        public $deliveryPrice;

        function  __construct($deli_type,$weight)
        {
            include '../Connections/conn.php';
			$sql="SELECT weight_start,weight_end,price
                  FROM sme_delivery_rate WHERE delivery_ids = $deli_type
                  ORDER BY weight_start ASC ";
				  
            $recordSql = mysql_query ( $sql, $conn );
            $i=0;
            while (  $rs = mysql_fetch_assoc ( $recordSql ) )
            {
                 $start = $weight >= $rs['weight_start'];
                 $end = $weight <= $rs['weight_end'];
                 $this->record[0][$i]= $rs['weight_start'];
                 $this->record[1][$i]= $rs['weight_end'];
                 $this->record[2][$i]= $rs['price'];
                 $i++;
            }
        
            $this->deliveryPrice=0;
            $this->delivery_Ids = $deli_type;
            $this->weight = $weight;
            $this->max = $this->limitWeight();
            $this->totalRows = mysql_num_rows($recordSql);

        }
       
        private function  calcDeliveryPrice($weight)
        {
            $price = 0;
            for ($i = 0; $i < count($this->record[0]); $i++)
            {
                $start = $weight >= $this->record[0][$i];
                $end  = $weight <= $this->record[1][$i];
                if( $start && $end )
                 {
                    $price =$price+$this->record[2][$i];;
                 }
            }
            
            return $price;
        }
       function limitWeight()
        {
          include '../Connections/conn.php';
		  $sql="SELECT MAX(weight_end)AS max
                FROM sme_delivery_rate
                WHERE delivery_ids = $this->delivery_Ids";
          $result = mysql_query ( $sql, $conn );
          $rs = mysql_fetch_assoc($result) ;
          return $rs['max'];
        }
        function fragment ()
        {
            $Piece = 1;
            $total_price =0;
            if($this->weight > $this->max)
            {
                $weight =$this->weight;
                $Piece = ceil($weight/$this->max);

                for ($i = 0; $i < $Piece; $i++)
                {
                    if($weight > $this->max)
                    {
                    $weight_fragment[$i]= $this->max;
                    $weight=$weight-$this->max;
                    }
                    else { $weight_fragment[$i]= $weight; }
                }              
            }
            else
            {
               $weight_fragment[0]= $this->weight;
            }

             foreach ($weight_fragment as $fragment)
             {
                
                $total_price += $this->calcDeliveryPrice($fragment);
             }
            return $total_price;
        }
        function deliveryPrice()
        {

        }
		

}



?>

